import logging

from pylons import request, session
from pylons.controllers.util import redirect_to, url_for

from homegrownauth.lib.base import BaseController

log = logging.getLogger(__name__)

class HomegrownController(BaseController):
    requires_auth = True

    def __before__(self):
        # Authentication required?
        if self.requires_auth and 'user' not in session:
            # Remember where we came from so that the user can be sent there
            # after a successful login
            session['path_before_login'] = request.path_info
            session.save()
            return redirect_to(url_for(controller='login'))

    def index(self):
        return ''

    def logout(self):
        """
        Logout the user and display a confirmation message
        """
        return redirect_to(url_for(controller='login/logout'))
